
package com.ahuiapp.ahuibang.engine;

import java.io.IOException;
import java.net.HttpURLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.Html;

public class HUIEngine {
	//webapi地址
	private String WebAPI_Path = "http://wap.unidust.cn/api/searchout.do?type=wap&ch=1001&info=";
	//private String WebAPI_Path = "http://uzoo.cn/search.do?type=wap&info=";
	//进度提示框
	private ProgressDialog mProgressDialog;
	//http对象，用于模仿访问
	private HttpClient httpClient = null;
	boolean isTextMode=true;
	boolean mIsTTsInstall=true;
	Handler mHandler;
	Handler mCallback;
	String results = "";
	Context mContext;
	LooperThread mThread;

	public HUIEngine(Context context) {
		mContext=context;

	}
	public void setSiriMode(boolean textmode){
		isTextMode=textmode;
	}

	public void handlerAnswer(String question, Handler handler) {
		mCallback = handler;
		results = "";
		if(mThread!=null&&mThread.isAlive())return;
		mProgressDialog =new ProgressDialog(mContext);
		if(isTextMode){//是否为文本模式
			mProgressDialog.setMessage("正在【辉云】上找答案，请稍等……");
		}else{
			mProgressDialog.setMessage("这个问题有点难，让我思考下-_-");
		}
		mProgressDialog.show();	
		mThread=new LooperThread(question);
		mThread.start();
	}

	@SuppressWarnings("finally")
	public String getResult(String question) {
		String strResult = null;
		HttpParams httpParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
		HttpConnectionParams.setSoTimeout(httpParams, 30000);
		httpClient = new DefaultHttpClient(httpParams);
		try {
			String strQuestion = WebAPI_Path + question;
			HttpGet httpRequest = new HttpGet(strQuestion);
			HttpResponse httpResponse = httpClient.execute(httpRequest);
			if (httpResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
				String str = EntityUtils.toString(httpResponse.getEntity());
				strResult = new String(str.getBytes("ISO-8859-1"), "UTF-8");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			return strResult;
		}
	}
	
	@SuppressWarnings("finally")
	public String getAPPVer() {
		String strResult = "暂无信息";
		HttpParams httpParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
		HttpConnectionParams.setSoTimeout(httpParams, 30000);
		httpClient = new DefaultHttpClient(httpParams);
		try {
			String url = "http://www.iyouwe.tk/fileup/ver.json";
			HttpGet httpRequest = new HttpGet(url);
			HttpResponse httpResponse = httpClient.execute(httpRequest);
			if (httpResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
				String str = EntityUtils.toString(httpResponse.getEntity());
				strResult = new String(str.getBytes("ISO-8859-1"), "UTF-8");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			return strResult;
		}
	}
	public String getAnswer(String question){
		String result=getResult(question);
		int firstIndex=result.indexOf("<br/>")+5;
		int lastIndex=result.lastIndexOf("<br/>");
		return Html.fromHtml(result.substring(firstIndex,lastIndex)).toString();
	}

	public void handleAnswer(String question) {
		mProgressDialog =new ProgressDialog(mContext);
		mProgressDialog.setMessage("这个问题有点难，让我好好想想");
		mProgressDialog.setCancelable(false);
		mProgressDialog.show();	
	}

	/*用于返回查询到的信息，以线程方式执行
	 * @author Administrator
	 *
	 */
	class LooperThread extends Thread {
		String mQuestion;
		public LooperThread(String question) {
			mQuestion = question;
		}
		public void run() {
			if(isTextMode){
				results="你太有才了";
				try{
					results=getAnswer(mQuestion).toString();
				}catch(Exception e){
					results="你太有才了，问得都是我不知道的问题,我不知道啊";
				}
			}
			Message message = new Message();
			message.what = 2012;
			message.obj = results;
			mCallback.sendMessage(message);	
			mProgressDialog.dismiss();
		}
	}
}
